package calibration;

import game.Constants;
import game.TwistyBasicGameState;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import vision.Vision;

/**
 * Calibrate Screen 
 * @author Twisty
 *
 */
public class CalibrateScreen extends TwistyBasicGameState {
	
	//Draws a white block when true and a black when false
	private boolean white = true;
	//Declares whether a new screen needs to be saved as an image
	private boolean setNewScreen = true;
	
	private boolean setCalibration = true;
	//The BufferedImage which will be saved as an image
	private BufferedImage screen;
	
	public CalibrateScreen(Vision of) {
		super(of);
		screen = new BufferedImage(Constants.screenWidth, Constants.screenHeight, BufferedImage.TYPE_INT_RGB);
	}
	
	@Override
	public void render(GameContainer container, StateBasedGame game, Graphics g)
			throws SlickException {
		Graphics2D g2 = screen.createGraphics();
		double blockWH;
		//Choose the smallest block dimensions
		if(blockWidth() < blockHeight()){
			blockWH = blockWidth();
		}else{
			blockWH = blockHeight();
		}
		
		g2.setColor(java.awt.Color.white);
		g.setColor(Color.white);
		g2.fillRect(0, 0, Constants.screenWidth, Constants.screenHeight);
		g.fillRect(0, 0, Constants.screenWidth, Constants.screenHeight);
		
		//Draw the blocks
		for(double y = 0; y < (double)Constants.blocksVer; y++){
			if(y%2 == 0){
				white = true;
			}else{
				white = false;
			}
			
			for(double x = 0; x < (double)Constants.blocksHor; x++){
				if(white){
					g.setColor(Color.white);
					g2.setColor(java.awt.Color.white);
				}else{
					g.setColor(Color.black);
					g2.setColor(java.awt.Color.black);
				}
				g.fillRect((int)(x*blockWH)+20,(int) (y*blockWH)+20, (int)blockWH, (int)blockWH);
				g2.fillRect((int)(x*blockWH)+20,(int) (y*blockWH)+20, (int)blockWH, (int)blockWH);
				white = !white;
			}
		}
		if(setNewScreen){
			createScreenAsImage();
			setNewScreen = false;
			setCalibration = true;
		}else{
			if(setCalibration){
				try {
					vision.calibrate();
				} catch (IOException e) {
					e.printStackTrace();
				}	
				setCalibration = false;
			}
		}
	}
	
	/**
	 * Creates the screen as an image.
	 */
	public void createScreenAsImage(){
		File f = new File(Constants.imageTargetPath);
		try{
			ImageIO.write(screen, "png", f);
		}catch(Exception e){
			System.out.println("asd");
		}
	}
	
	public void update(GameContainer container, StateBasedGame game, int delta)
			throws SlickException {
		super.update(container, game, delta);
		if(!setCalibration && this.vision.isCalibrated()){
			goToMenu = true;
		}
	}
	
	/**
	 * @return - The width of the blocks
	 */
	private double blockWidth(){
		return ((Constants.screenWidth-40)/(double)Constants.blocksHor);
	}
	
	@Override
	protected void goToMenu_event(){
		setNewScreen = true;
		setCalibration = true;
	}
	
	/**
	 * @return - The height of the blocks
	 */
	private double blockHeight(){
		return ((Constants.screenHeight-40)/(double)Constants.blocksVer);
	}

	@Override
	public int getID() {
		return Constants.calibrationScreen;
	}
}
